package com.System.LearnSystemTeacher;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.System.LearnSystem.Course;
import com.System.LearnSystem.Result;
import com.System.LearnSystem.Token;
import com.System.LearnSystem.sqlConnection;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;

import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

class Message   //用于临时储存客户端发来的请求信息
{
	private String token;
	private String role;
	private String course_name;
	private String course_no;
	
	public Message()
	{
		
	}
	
	public String gettoken()
	{
		return token;
	}
	public void settoken(String token)
	{
		this.token = token;
	}
	
	public String getrole()
	{
		return role;
	}
	public void setrole(String role)
	{
		this.role = role;
	}
	
	public String getcourse_name()
	{
		return course_name;
	}
	public void setcourse_name(String course)
	{
		this.course_name = course;
	}
	
	public String getcourse_no()
	{
		return course_no;
	}
	public void setcourse_no(String course_no)
	{
		this.course_no = course_no;
	}

}

@WebServlet("/tea/search")
public class searchCourse extends HttpServlet{
	
	public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException
	{

		StringBuilder json = new StringBuilder();//读取json请求
        BufferedReader reader = req.getReader();
        String line;
        while ((line = reader.readLine()) != null) {
            json.append(line);
        }
        String jsonStr = json.toString();     
        ObjectMapper objectMapper = new ObjectMapper();
        Message message = objectMapper.readValue(jsonStr, Message.class);
        
        objectMapper.registerModule(new JavaTimeModule());//注册驱动保证序列化
        res.setContentType("application/json;charset=UTF-8");//设置响应格式
        PrintWriter out = res.getWriter();
        
        if(message.gettoken()!=null)
        {
        	if(Token.checkToken(message.gettoken())&&"TEACHER".equals(message.getrole()))//验证令牌和身份
        	{
        		System.out.println(message.getcourse_no());
        		System.out.println(message.getcourse_name());
        		String sql = "SELECT * FROM t_course WHERE course_no = ? or course_name = ?";//预处理语句
        		try (Connection conn = sqlConnection.getConnection();PreparedStatement pstmt = conn.prepareStatement(sql))
        		{
        			pstmt.setString(1,message.getcourse_no());//将对应信息替换？的位置
        			pstmt.setString(2,message.getcourse_name());
        			ResultSet rs = pstmt.executeQuery();
        			Course course = new Course();
        			while(rs.next())
        			{
        				course.setId(rs.getLong("id"));
	        			course.setCourseNo(rs.getString("course_no"));
	        			course.setCourseName(rs.getString("course_name"));
	        			course.setTeacherId(rs.getLong("teacher_id"));
	        			course.setMajorId(rs.getLong("major_id"));
	        			course.setGrade(rs.getInt("grade"));
	        			course.setCourseType(rs.getInt("course_type"));
	        			course.setCredit(rs.getBigDecimal("credit"));
	        			course.setStatus(rs.getInt("status"));
	        			course.setIsPublic(rs.getInt("is_public"));
	        			Result<Course> result = Result.success(course);
	        			out.println(objectMapper.writeValueAsString(result));
        			}
        			if(course.getCourseName()==null)
        			{
        				Result<Object> result = Result.error(403,"未查询到该课程");
                		out.println(objectMapper.writeValueAsString(result));
        			}
        			
        		} catch (SQLException e) {
        			System.err.println("SQL 错误码: " + e.getErrorCode());
          		  	System.err.println("SQL 错误消息: " + e.getMessage());
          		    e.printStackTrace();
				}
        	}
        	else
        	{
        		Result<Object> result = Result.error(401,"非授权访问");
        		out.println(objectMapper.writeValueAsString(result));
        	}
        }
        else
        {
        	Result<Object> result = Result.error(401,"非授权访问");
        	out.println(objectMapper.writeValueAsString(result));
        }
	}
}














